import java.io.*;
import java.net.*;
/**
 * @author: gao
 * @create-date: 2025/5/22 20:18
 */

public class Client {
    public static void main(String[] args) {
        String serverAddress = "localhost";
        int port = 5_000;
        Socket socket = null;

        try {
            socket = new Socket(serverAddress, port);
            System.out.println("Connected to server at " + serverAddress + ":" + port);

            InputStream is = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String message = br.readLine();
            System.out.println("Received from server: " + message);

            socket.close();
            System.out.println("Connection to server closed.");

        } catch (UnknownHostException e) {
            System.err.println("Unknown host: " + serverAddress);
        } catch (IOException e) {
            System.err.println("Client error: " + e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                if (socket != null && !socket.isClosed()) {
                    socket.close();
                }
            } catch (IOException e) {
                System.err.println("Error closing client socket: " + e.getMessage());
            }
        }
    }
}